package com.starhub.domain.annotate.builder;

import java.time.LocalDateTime;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.starhub.application.agent.dto.AIChatInfo;
import com.starhub.application.agent.result.AIAgentResult;
import com.starhub.application.agent.service.IChatService;
import com.starhub.common.bean.agent.AgentConfig;
import com.starhub.common.security.util.SecurityUtils;
import com.starhub.domain.annotate.constants.DataAnnotateConstants;
import com.starhub.domain.annotate.dto.DataAnnotatePrompt;
import com.starhub.utils.DtoToMapConverter;

/**
 * 构建对话信息
 * 
 * @version: V1.0
 * @author: liuhf
 * @time 2025-06-18
 */
@Component
public class ChatInfoBuilder {

    @Autowired
    private IChatService chatService;

    public AIChatInfo buildChatInfo(DataAnnotatePrompt dataAnnotationPrompt) {
        String userId = SecurityUtils.getCurrentUserId();
        return buildChatInfo(dataAnnotationPrompt, userId);
    }

    public AIChatInfo buildChatInfo(DataAnnotatePrompt dataAnnotationPrompt, String userId) {
        Map<String, Object> additionalParams = DtoToMapConverter.convertWithNull(dataAnnotationPrompt);

        AIAgentResult agentResult = chatService.initAgentById(DataAnnotateConstants.DATA_ANNOTATION_AGENT);

        AgentConfig agentConfig = agentResult.getAgentConfig();
        String sessionId = agentResult.getSessionId();

        AIChatInfo chatInfo = new AIChatInfo();
        chatInfo.setAdditionalParams(additionalParams);
        chatInfo.setUserId(userId);
        chatInfo.setAgentId(agentConfig.getId());
        chatInfo.setSessionId(sessionId);
        chatInfo.setUserMessage("");
        chatInfo.setSelectedModel(agentConfig.getFixedModel());
        chatInfo.setRequestTime(LocalDateTime.now());
        return chatInfo;
    }
}
